﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Krile.Kernel.WindowsEx;

namespace Krile.Data
{
    public partial class DSDataArgSetUI : Krile.Kernel.Control.SDataArgSetUI
    {
        public override void Accepted()
        {
            gSearchDataBase.Arguments = searchText.Text;
            gSearchDataBase.SearchType = (DefaultSearchDataBase.SearchTypes)searchTypeSelector.SelectedIndex;
            gSearchDataBase.IgnoreCase = ignoreCase.Checked;
        }

        //argument
        public string SearchArgument
        {
            get { return searchText.Text; }
            set { searchText.Text = value; }
        }

        public Data.DefaultSearchDataBase.SearchTypes SearchType
        {
            get { return (DefaultSearchDataBase.SearchTypes)searchTypeSelector.SelectedIndex; }
            set
            {
                searchTypeSelector.SelectedIndex = (int)value;
            }
        }

        public bool IgnoreCase
        {
            get { return ignoreCase.Checked; }
            set { ignoreCase.Checked = value; }
        }

        Data.DefaultSearchDataBase gSearchDataBase;
        public DSDataArgSetUI(Data.DefaultSearchDataBase gsd)
        {
            InitializeComponent();
            gSearchDataBase = gsd;
            searchText.Text = gSearchDataBase.Arguments;
            searchTypeSelector.SelectedIndex = (int)gSearchDataBase.SearchType;
            ignoreCase.Checked = gsd.IgnoreCase;
        }

        private void searchText_TextChanged(object sender, EventArgs e)
        {
            if (((DefaultSearchDataBase.SearchTypes)searchTypeSelector.SelectedIndex) == DefaultSearchDataBase.SearchTypes.Regex)
            {
                try
                {
                    Regex rgx = new Regex(searchText.Text);
                    regexErrorProvider.Clear();
                }
                catch (ArgumentException ae)
                {
                    regexErrorProvider.SetError(searchText, Lang.DSData.RegexError + ae.Message);
                }
            }
        }

        private void searchTypeSelector_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((DefaultSearchDataBase.SearchTypes)searchTypeSelector.SelectedIndex) == DefaultSearchDataBase.SearchTypes.Regex)
            {
                try
                {
                    Regex rgx = new Regex(searchText.Text);
                    regexErrorProvider.Clear();
                }
                catch (ArgumentException ae)
                {
                    regexErrorProvider.SetError(searchText, Lang.DSData.RegexError + ae.Message);
                }
            }
        }
    }
}
